package com.surfilter.massdata.spark.model;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.search.SearchHit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.surfilter.massdata.es.core.entity.EsEntity;
import com.surfilter.massdata.es.core.util.EsUtil;
import com.surfilter.massdata.spark.bean.massDomain.FullDomainIndex;

public class EsFullDomainVisits implements EsEntity<FullDomainIndex> {
	
	private static final Logger log = LoggerFactory.getLogger(EsFullDomainVisits.class);
	
	@Override
	public XContentBuilder createMappingJson(String typeName) throws IOException {
		XContentBuilder mapping = jsonBuilder()
				.startObject()
					.startObject(typeName)
						.startObject("_source").field("enabled", true).endObject()
						.startObject("_all").field("enabled", false).endObject()
							.startObject("properties")
								.startObject("ymid").field("type","long").endObject()
								.startObject("ym").field("type","string").field("index","not_analyzed").endObject()
								.startObject("ymSuffix").field("type","string").field("index","not_analyzed").endObject()
								.startObject("siteName").field("type","string").field("index","not_analyzed").endObject()
								.startObject("shengIds").field("type","long").field("index","no").endObject()
								.startObject("shiIds").field("type","long").endObject()
								.startObject("xianIds").field("type","long").field("index","no").endObject()
								.startObject("fxsj").field("type","date").field("format","yyyy-MM-dd HH:mm:ss").field("index","no").endObject()
								.startObject("jrsIds").field("type","long").endObject()
								.startObject("jrsMcs").field("type","string").field("index","no").endObject()
								.startObject("wzjb").field("type","long").field("index","not_analyzed").endObject()
								.startObject("ymzcsMc").field("type","string").field("index","not_analyzed").endObject()
								.startObject("sfch").field("type","long").endObject()
								.startObject("houseCodes").field("type","string").field("index","not_analyzed").endObject()
								.startObject("houseNames").field("type","string").field("index","no").endObject()
								.startObject("djym").field("type","String").field("index","not_analyzed").endObject()
								.startObject("gxsj").field("type","date").field("format","yyyy-MM-dd HH:mm:ss").field("index","no").endObject()
								.startObject("ips").field("type","string").field("index","not_analyzed").endObject()
								.startObject("idcIspIds").field("type","string").field("index","not_analyzed").endObject()
								.startObject("yysMcs").field("type","string").field("index","no").endObject()
								.startObject("isRecord").field("type","string").field("index","not_analyzed").endObject()
								.startObject("domainCN").field("type","string").field("index","no").endObject()
								.startObject("hostUnit").field("type","string").field("index","not_analyzed").endObject()
								.startObject("license").field("type","string").field("index","no").endObject()
								.startObject("domainValidity").field("type","date").field("format","yyyy-MM-dd HH:mm:ss").field("index","no").endObject()
					.endObject()
				.endObject();
		return mapping;
	}

	@Override
	public XContentBuilder buildEntity(FullDomainIndex entity) throws IOException {/*
		// ip 字段非空
		String[] arrIp = entity.getIps().split(",");
		int arrSize = arrIp.length;
		if(arrSize > 10000){
			log.warn("into es ips :{},ips:{}",arrSize, entity.getIps());
			arrSize = 10000;
		}
		
		String splitStr = ",";
		
		Object[] arrShengId = EsUtil.transformLongArr(entity.getShengIds(),splitStr,arrSize);
		Object[] arrShiId = EsUtil.transformLongArr(entity.getShiIds(),splitStr,arrSize);
		Object[] arrXianId = EsUtil.transformLongArr(entity.getXianIds(),splitStr,arrSize);
		Object[] arrJrsId = EsUtil.transformLongArr(entity.getJrsIds(),splitStr,arrSize);
		String[] arrJrsMc = EsUtil.transformStringArr(entity.getJrsMcs(),splitStr,arrSize);
		String[] arrHousCode = EsUtil.transformStringArr(entity.getHouseCodes(),splitStr,arrSize);
		String[] arrHouseName = EsUtil.transformStringArr(entity.getHouseNames(),splitStr,arrSize);
		String[] arrIdcIspId = EsUtil.transformStringArr(entity.getIdcIspIds(),splitStr,arrSize);
		String[] arrYysMc = EsUtil.transformStringArr(entity.getYysMcs(),splitStr,arrSize);
		XContentBuilder source = null;
		try {
			 source = XContentFactory.jsonBuilder().startObject()
					.field("ymid" , entity.getYmid())
					.field("ym" , EsUtil._prefix + entity.getYm())
					.field("ymSuffix" , entity.getYmSuffix())
					.field("siteName" , EsUtil._prefix + (entity.getSiteName() == null ? "" : entity.getSiteName()))
					.array("shengIds" , arrShengId)
					.array("shiIds" , arrShiId)
					.array("xianIds" , arrXianId)
					.field("fxsj" , entity.getFxsj())
					.array("jrsIds" , arrJrsId)
					.array("jrsMcs" , arrJrsMc)
					.field("wzjb" , entity.getWzjb())
					.field("ymzcsMc" , EsUtil._prefix + entity.getYmzcsMc())
					.field("sfch" , entity.getSfch())
					.field("houseCodes" , arrHousCode)
					.field("houseNames" , arrHouseName)
					.field("djym" , EsUtil._prefix + entity.getDjym())
					.field("gxsj" , entity.getGxsj())
					.array("ips" , arrIp)
					.array("idcIspIds" , arrIdcIspId)
					.array("yysMcs" , arrYysMc)
					.field("isRecord" , entity.getIsRecord())
					.field("domainCN" , entity.getDomainCN())
					.field("hostUnit" , entity.getHostUnit())
					.field("license" , entity.getLicense())
					.field("domainValidity" , entity.getDomainValidity())
				.endObject();
		} catch (Exception e) {
			log.error("data to entity error..", e);
		}

		return source;
	*/
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public FullDomainIndex recoveEntity(Map<String, Object> propertiesMap) {/*
		FullDomainIndex entity = new FullDomainIndex();
		for (String key : propertiesMap.keySet()) {
			if ("ymid".equals(key)){
				 entity.setYmid(Long.valueOf(propertiesMap.get(key).toString()));
			}
			else if ("ym".equals(key)){
				 String ym = (String)propertiesMap.get(key);
				 if (StringUtils.isNotBlank(ym)) {
					 entity.setYm(ym.substring(1));
				 }else {
					 entity.setYm(null);
				 }
			}else if ("ymSuffix".equals( key ) ) {
				 String ymSuffix = (String)propertiesMap.get(key);
				 if (StringUtils.isNotBlank(ymSuffix)) {
					 entity.setYm(ymSuffix);
				 }else {
					 entity.setYm(null);
				 }
			}
			 else if ("siteName".equals( key ) ) {
				 String siteName = (String)propertiesMap.get(key);
				 if (StringUtils.isNotBlank(siteName)) {
					 entity.setSiteName(siteName.substring(1));
				 }else {
					 entity.setSiteName(null);
				 }
			}
			 else if ("shengIds".equals( key ) ) {
				 List<Long> lstIps = (List<Long>) propertiesMap.get(key);
				 StringBuffer sb = new StringBuffer();
				 for (int i = 0 ,size = lstIps.size(); i < size; i++) {
					 sb.append(lstIps.get(i));
					 if (i < size-1 ) {
						sb.append(",");
					}
				 }
				 entity.setShengIds(sb.toString());
				 sb.delete(0, sb.length());
			}
			 else if ("shiIds".equals( key ) ) {
				 List<Long> lstIps = (List<Long>) propertiesMap.get(key);
				 StringBuffer sb = new StringBuffer();
				 for (int i = 0 ,size = lstIps.size(); i < size; i++) {
					 sb.append(lstIps.get(i));
					 if (i < size-1 ) {
						sb.append(",");
					}
				 }
				 entity.setShiIds(sb.toString());
				 sb.delete(0, sb.length());
			}
			 else if ("xianIds".equals( key ) ) {
				 List<Long> lstIps = (List<Long>) propertiesMap.get(key);
				 StringBuffer sb = new StringBuffer();
				 for (int i = 0 ,size = lstIps.size(); i < size; i++) {
					 sb.append(lstIps.get(i));
					 if (i < size-1 ) {
						sb.append(",");
					}
				 }
				 entity.setXianIds(sb.toString());
				 sb.delete(0, sb.length());
			}
			 else if ("fxsjs".equals( key ) ) {
				 entity.setFxsj((String)propertiesMap.get(key));
			}
			 else if ("jrsIds".equals( key ) ) {
				 List<Long> lstIps = (List<Long>) propertiesMap.get(key);
				 StringBuffer sb = new StringBuffer();
				 for (int i = 0 ,size = lstIps.size(); i < size; i++) {
					 sb.append(lstIps.get(i));
					 if (i < size-1 ) {
						sb.append(",");
					}
				 }
				 entity.setJrsIds(sb.toString());
				 sb.delete(0, sb.length());
			}
			 else if ("jrsMcs".equals( key ) ) {
				 List<String> lstIps = (List<String>) propertiesMap.get(key);
				 StringBuffer sb = new StringBuffer();
				 for (int i = 0 ,size = lstIps.size(); i < size; i++) {
					 sb.append(lstIps.get(i));
					 if (i < size-1 ) {
						sb.append(",");
					}
				 }
				 entity.setJrsMcs(sb.toString());
				 sb.delete(0, sb.length());
			}
			 else if ("wzjb".equals( key ) ) {
				 if (propertiesMap.get(key) != null &&
						 StringUtils.isNotBlank(propertiesMap.get(key).toString())) {
					 entity.setWzjb(Long.valueOf(propertiesMap.get(key).toString()));
				 } else {
					 entity.setWzjb(null);
				 }
			}
			 else if ("ymzcsMc".equals( key ) ) {
				 String ymzcsMc = (String)propertiesMap.get(key);
				 if (StringUtils.isNotBlank(ymzcsMc)) {
					 entity.setYmzcsMc(ymzcsMc.substring(1));
				 }else {
					 entity.setYmzcsMc(null);
				 }
			}
			 else if ("sfch".equals( key ) ) {
				 if (propertiesMap.get(key) != null &&
						 StringUtils.isNotBlank(propertiesMap.get(key).toString())) {
					 entity.setSfch(Long.valueOf(propertiesMap.get(key).toString()));
				 } else {
					 entity.setSfch(null);
				 }
			}
			 else if ("houseCodes".equals( key ) ) {
				 List<String> lstHouseCode = (List<String>) propertiesMap.get(key);
				 StringBuffer sb = new StringBuffer();
				 for (int i = 0 ,size = lstHouseCode.size(); i < size; i++) {
					 sb.append(lstHouseCode.get(i));
					 if (i < size-1 ) {
						sb.append(",");
					}
				 }
				 entity.setHouseCodes(sb.toString());
				 sb.delete(0, sb.length());
			}
			 else if ("houseNames".equals( key ) ) {
				 List<String> lsthouseName = (List<String>) propertiesMap.get(key);
				 StringBuffer sb = new StringBuffer();
				 for (int i = 0 ,size = lsthouseName.size(); i < size; i++) {
					 sb.append(lsthouseName.get(i));
					 if (i < size-1 ) {
						sb.append(",");
					}
				 }
				 entity.setHouseNames(sb.toString());
				 sb.delete(0, sb.length());
			}
			 else if ("djym".equals( key ) ) {
				 String djym = (String)propertiesMap.get(key);
				 if (StringUtils.isNotBlank(djym)) {
					 entity.setDjym(djym.substring(1));
				 }else {
					 entity.setDjym(null);
				 }
			}
			 else if ("gxsj".equals( key ) ) {
				 entity.setGxsj((String)propertiesMap.get(key));
			}
			 else if ("ips".equals( key ) ) {
				 List<String> lstIps = (List<String>) propertiesMap.get(key);
				 StringBuffer sb = new StringBuffer();
				 for (int i = 0 ,size = lstIps.size(); i < size; i++) {
					 sb.append(lstIps.get(i));
					 if (i < size-1 ) {
						sb.append(",");
					}
				 }
				 entity.setIps(sb.toString());
				 sb.delete(0, sb.length());
			}
			 else if ("idcIspIds".equals( key ) ) {
				 List<String> lstIps = (List<String>) propertiesMap.get(key);
				 StringBuffer sb = new StringBuffer();
				 for (int i = 0 ,size = lstIps.size(); i < size; i++) {
					 sb.append(lstIps.get(i));
					 if (i < size-1 ) {
						sb.append(",");
					}
				 }
				 entity.setIdcIspIds(sb.toString());
				 sb.delete(0, sb.length());
			}
			 else if ("yysMcs".equals( key ) ) {
				 List<String> lstIps = (List<String>) propertiesMap.get(key);
				 StringBuffer sb = new StringBuffer();
				 for (int i = 0 ,size = lstIps.size(); i < size; i++) {
					 sb.append(lstIps.get(i));
					 if (i < size-1 ) {
						sb.append(",");
					}
				 }
				 entity.setYysMcs(sb.toString());
				 sb.delete(0, sb.length());
			}
			 else if ("isRecord".equals( key ) ) {
				 entity.setIsRecord((String)propertiesMap.get(key));
			}
			 else if ("domainCN".equals( key ) ) {
				 entity.setDomainCN((String)propertiesMap.get(key));
			}
			 else if ("hostUnit".equals( key ) ) {
				 entity.setHostUnit((String)propertiesMap.get(key));
			}
			 else if ("license".equals( key ) ) {
				 entity.setLicense((String)propertiesMap.get(key));
			}
			 else if ("domainValidity".equals( key ) ) {
				 entity.setDomainValidity((String)propertiesMap.get(key));
			}
		}
		return entity;
	*/	
		return null;
	}

	@Override
	public XContentBuilder buildUpdateEntity(FullDomainIndex entity) throws IOException {
		return null;
	}

	@Override
	public FullDomainIndex recoveEntityAndIndex(Map<String, Object> propertiesMap,SearchHit arrHits) {
		return null;
	}

}
